package club.zhcs.sbc.conf.log4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.log4j.PropertyConfigurator;
import org.springframework.util.StringUtils;

/**
 * 通过-Dlog.config=path传入log4j的配置感知
 * 
 * @author guiyuan.wang-N
 *
 *         createTime 2016年12月30日下午1:15:29
 *
 */
@WebListener
public class Log4jConfigListener implements ServletContextListener {

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * javax.servlet.ServletContextListener#contextInitialized(javax.servlet.
	 * ServletContextEvent)
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		String logConfig = System.getProperty("log.config");
		if (!StringUtils.isEmpty(logConfig)) {
			PropertyConfigurator.configureAndWatch(logConfig, 100);// 执行后100毫秒生效
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
	 * ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}

}
